<template>
  <view class="pay">
    <!-- 1 收货地址 -->
    <view class="address u-p-20">
      <view v-if="!addressInfo" class="address-btn u-flex u-row-center">
        <u-button
          shape="circle"
          type="error"
          plain
          size="medium"
          @click="checkedAddress"
          :custom-style="{ backgroundColor: '#fff!important' }"
          >获取收货地址</u-button
        >
      </view>

      <view v-else class="address-detail">
        <view class="address-detail">{{ addressDetail }}</view>
        <view class="address-user"
          >{{ addressInfo.userName }}: {{ addressInfo.telNumber }}</view
        >
      </view>
    </view>

    <u-line color="#eee"></u-line>

    <!-- 2 已选商品 -->
    <view class="goods-list u-p-15">
      <view class="goods-list-title">已选商品</view>
      <view class="goods-list-content">
        <view v-for="item in checkList" :key="item.goods_id" class="cart-item">
          <!-- 2 图片 -->
          <view class="goods-img">
            <u-image
              :src="item.goods_small_logo"
              width="191rpx"
              height="191rpx"
            ></u-image>
          </view>
          <!-- 3 信息 -->
          <view class="goods-info">
            <!-- 名称 -->
            <view class="goods-name u-line-2">{{ item.goods_name }}</view>
            <!-- 价格和数量 -->
            <view class="goods-price-num">
              <view class="goods-price">￥{{ item.goods_price }}</view>
              <view class="goods-num-tool">
                <view class="goods-vnum">X{{ item.num }}</view>
              </view>
            </view>
          </view>
        </view>
      </view>
    </view>

    <!-- 2 统计 -->
    <view class="statistics">
      <view class="all-price">
        合计: <text>￥{{ priceTotal }}</text>
      </view>
      <view class="all-count">
        <u-button type="error" @click="handlePay" shape="circle"
          >去支付({{ cartTotal }})</u-button
        >
      </view>
    </view>
  </view>
</template>

<script>
import { mapGetters, mapActions } from "vuex";
export default {
  data() {
    return {
      addressInfo: null,
    };
  },
  computed: {
    ...mapGetters("cart", [
      "checkList",
      "cartTotal",
      "priceTotal",
      "checkList",
    ]),
    ...mapGetters("user", ["token"]),
    addressDetail() {
      if (!this.addressInfo) return "";
      const { provinceName, cityName, countyName, detailInfo } =
        this.addressInfo;
      return provinceName + cityName + countyName + detailInfo;
    },
  },
  methods: {
    ...mapActions("user", ["fetchToken"]),
    async checkedAddress() {
      const [err, res] = await uni.chooseAddress();
      if (err) {
        console.log(err);
        uni.showToast({
          title: "无法获取收货地址！！",
          icon: "none",
        });

        return;
      }
      this.addressInfo = res;
    },
    async handlePay() {
      if (!this.addressInfo) {
        uni.showToast({
          title: "请选择收货地址",
          icon: "none",
        });
        return;
      }

      if (!this.token) {
          await this.fetchToken();
      }


      //创建订单编号
      const orderParams = {
        order_price: this.priceTotal,
        consignee_addr: this.addressDetail,
        goods: this.checkList.map((goods) => ({
          goods_id: goods.goods_id,
          goods_number: goods.num,
          goods_price: goods.goods_price,
        })),
      };

      const resOrder = await this.$u.http.post(
        "/my/orders/create",
        orderParams,
      );

      const { order_number } = resOrder;

      //  获取支付参数
      const resParams = await this.$u.http.post(
        "/my/orders/req_unifiedorder",
        { order_number },
      );

      const { pay } = resParams;
      //支付
      const [err2, result] = await uni.requestPayment(pay);
    },
  },
};
</script>

<style lang="scss">
.pay {
  padding-bottom: 111rpx;
}
.cart-item {
  display: flex;
  padding: 10rpx;
  border-bottom: 1rpx solid #ccc;

  .goods-img {
    flex: 2;

    display: flex;
    align-items: center;
    justify-content: center;
  }

  .goods-info {
    color: #666;
    flex: 3;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    .goods-name {
    }

    .goods-price-num {
      display: flex;
      justify-content: space-between;
      .goods-price {
        color: $theme-color;
      }

      .goods-num-tool {
        width: 140rpx;
        height: 40rpx;

        display: flex;
        justify-content: flex-end;

        .goods-vnum {
        }
      }
    }
  }
}
.statistics {
  position: fixed;
  background-color: #fff;
  bottom: 0;
  left: 0;
  width: 100%;
  z-index: 100;
  border-top: 1rpx solid #ccc;
  display: flex;
  align-items: center;
  padding: 15rpx;
  .all-price {
    flex: 1;
    color: #666;
    text {
    }
  }

  .all-count {
  }
}
</style>